<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    var data = [
      {
        id: 1,
        label: "一级 1",
        children: [
          {
            id: 4,
            label: "二级 1-1",
            children: [
              {
                id: 9,
                label: "三级 1-1-1",
              },
              {
                id: 10,
                label: "三级 1-1-2",
              },
            ],
          },
        ],
      },
      {
        id: 2,
        label: "一级 2",
        children: [
          {
            id: 5,
            label: "二级 2-1",
          },
          {
            id: 6,
            label: "二级 2-2",
          },
        ],
      },
      {
        id: 3,
        label: "一级 3",
        children: [
          {
            id: 7,
            label: "二级 3-1",
          },
          {
            id: 8,
            label: "二级 3-2",
          },
        ],
      },
    ];
    function find(arr, label) {
      let stock = [];
      let going = true;
      var warker = function (arr, label) {
        arr.forEach((item) => {
          if (!going) return;
          stock.push(item.label);
          if (item.label == label) {
            going = false;
          } else if (item.children) {
            warker(item.children, label);
          } else {
            stock.pop();
          }
        });
        if (going) stock.pop();
      };
      warker(arr, label);
      return stock;
    }
    console.log(find(data, "三级 1-1-2"));

    function formatTree(arr, levelInfo = "") {
      return arr.map((item) => {
        const newParent = levelInfo ? levelInfo + "--" + item.id : "" + item.id;
        const temp = {
          ...item,
          levelInfo: newParent,
        };
        if (item.children) {
          temp.children = formatTree(item.children, newParent);
        }
        return temp;
      });
    }
    console.log(formatTree(data));
  </script>
</html>
